﻿using System;
using Microsoft.DirectX.DirectSound;

namespace breakoutsharp.sound
{
    public class DxSoundPlayer : IDisposable
    {
        protected Device device;
        protected SecondaryBuffer sound;
        private bool disposed = false;


        public DxSoundPlayer(IntPtr handle, string file)
        {
            device = new Device();
            device.SetCooperativeLevel(handle, CooperativeLevel.Normal);

            sound = new SecondaryBuffer(file, device);
        }

        public void Play()
        {
            sound.Play(0, BufferPlayFlags.Default);
        }

        // IDisposable
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    // TODO: free other state (managed objects).
                    if ((sound != null))
                    {
                        sound.Dispose();
                    }
                    sound = null;

                    if ((device != null))
                    {
                        device.Dispose();
                    }
                    device = null;
                }
            }

            // TODO: free your own state (unmanaged objects).
            // TODO: set large fields to null.
            this.disposed = true;
        }

        #region " IDisposable Support "
        // This code added by Visual Basic to correctly implement the disposable pattern.
        public void Dispose()
        {
            // Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion

    }
}
